
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Helper;
using IRepository;
using Models;
using System.Linq;
using Models.Entity;
using MySql.Data.MySqlClient;
using Dapper;
using System.Data;
using System.Linq.Expressions;

namespace Repository
{
    /// <summary>
    /// 图片处理服务
    /// </summary>
    public class ClassesRepository : IClassesRepository
    {
        DapperHelper<CClassesEntity> dbHelper = new DapperHelper<CClassesEntity>();

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key">主键 ID</param>
        /// <returns></returns>
        public async Task<ResultObj<int>> Delete(int key)
        {
            ResultObj<int> reObj = new ResultObj<int>();
            dbHelper.TranscationBegin();
            int opNum = 0;
            try
            {
                DapperHelper<CClassesWeekCourseEntity> cwc = new DapperHelper<CClassesWeekCourseEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction());
                opNum = await cwc.Delete(i => i.classes_id == key);

                DapperHelper<CClassesCfgTeacherClassesEntity> cctc = new DapperHelper<CClassesCfgTeacherClassesEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction());
                opNum = await cctc.Delete(i => i.classes_id == key);

                DapperHelper<CClassesCfgEntity> cc = new DapperHelper<CClassesCfgEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction());
                var allCfg = await cc.FindAll(x => x.classes_id == key);

                DapperHelper<CClassesCfgTeacherEntity> cct = new DapperHelper<CClassesCfgTeacherEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction());
                var allCfgIdList = allCfg.Select(x => x.classes_cfg_id).ToList();
                opNum = await cct.Delete(string.Format(" classes_cfg_id in({0})", string.Join(",", allCfgIdList)));

                opNum = await cc.Delete(i => i.classes_id == key);

                opNum = await dbHelper.Delete(i => i.classes_id == key);

                dbHelper.TranscationCommit();
            }
            catch (System.Exception)
            {
                dbHelper.TranscationRollback();
            }
            reObj.data = opNum;
            reObj.success = reObj.data > 0;
            return reObj;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="inEnt"></param>
        /// <returns></returns>
        public async Task<ResultObj<int>> Save(DtoSave<CClassesEntity> inEnt)
        {

            ResultObj<int> reObj = new ResultObj<int>();
            if (inEnt.data.classes_id == 0)
            {
                inEnt.data.classes_id = await SequenceRepository.GetNextID<CClassesEntity>();
                reObj.data = await dbHelper.Save(inEnt);
            }
            else
            {
                reObj.data = await dbHelper.Update(inEnt);
            }

            reObj.success = reObj.data > 0;
            return reObj;
        }
    }
}
